package com.nd.mobile.study.view;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.common.constants.BundleConstants;
import com.nd.mobile.study.domain.ClassInfoCatalog;
import com.nd.mobile.study.domain.HomeworkARecord;
import com.nd.mobile.study.view.adapter.SimpleListAdapter;

public class HomeworkRecordCatalogActivity extends BaseActivity {

	private ListView					mLV;
	private HomeworkRecordListAdapter	mAdapter;
	private OnItemClickListener			mListListener;
	private Intent						mIntent2Detail;

	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.simple_list);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		loadList();
	}

	@Override
	protected void setFieldValues() {
		super.setFieldValues();
		mIntent2Detail = new Intent().setClass(this, HomeworkRecordDetailActivity.class);
		mListListener = new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				HomeworkRecordListAdapter adapter = (HomeworkRecordListAdapter) parent.getAdapter();
				HomeworkARecord record = adapter.getData().get(position);
				mIntent2Detail.putExtra(BundleConstants.HOMEWORK_A_RECORD, record);
				startActivity(mIntent2Detail);
			}
		};
		mLV.setOnItemClickListener(mListListener);
	}

	@Override
	protected void initViews() {
		mLV = (ListView) findViewById(R.id.common_lv);
	}

	private void loadList() {
		mLoading.p();
		new GetHomeworkRecordTask(this).execute();
	}

	private class GetHomeworkRecordTask extends SimpleAsyncTask<Void, Void, HomeworkARecord[]> {

		public GetHomeworkRecordTask(Context context) {
			super(context);
		}

		@Override
		protected HomeworkARecord[] onLoad(Void... params) throws Exception {
			return App.getInstance().getUser().getHomeworkARecord();
		}

		@Override
		protected void onUI(HomeworkARecord[] result) {
			mAdapter = new HomeworkRecordListAdapter(mContext, result);
			mLV.setAdapter(mAdapter);
			mAdapter.notifyDataSetChanged();
		}
	}

	private class HomeworkRecordListAdapter extends SimpleListAdapter<HomeworkARecord> {

		public HomeworkRecordListAdapter(Context context, List<HomeworkARecord> data) {
			super(context, data);
		}

		public HomeworkRecordListAdapter(Context context, HomeworkARecord[] data) {
			super(context, data);
		}

		private class ViewHolder {
			TextView	tvTitle;
			TextView	tvScore;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (null == convertView) {
				convertView = mInflater.inflate(R.layout.homework_a_record_list_item, null);

				holder = new ViewHolder();
				holder.tvTitle = (TextView) convertView.findViewById(R.id.homework_a_record_catalog_title);
				holder.tvScore = (TextView) convertView.findViewById(R.id.homework_a_record_catalog_score);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.tvTitle.setText(mData.get(position).getTitle());
			holder.tvScore.setText(mData.get(position).getScore());

			return convertView;
		}
	}
}
